<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
  <title>Description of fitPowerLaw</title>
  <meta name="keywords" content="fitPowerLaw">
  <meta name="description" content="This function fits a power law of the form X = aii*z^n to the given data">
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta name="generator" content="m2html &copy; 2005 Guillaume Flandin">
  <meta name="robots" content="index, follow">
  <link type="text/css" rel="stylesheet" href="../../m2html.css">
  <script type="text/javascript">
    if (top.frames.length == 0) { top.location = "../../index.html"; };
  </script>
</head>
<body>
<a name="_top"></a>
<!-- ../menu.html VMT_4.0_dev --><!-- menu.html utils -->
<h1>fitPowerLaw
</h1>

<h2><a name="_name"></a>PURPOSE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>This function fits a power law of the form X = aii*z^n to the given data</strong></div>

<h2><a name="_synopsis"></a>SYNOPSIS <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="box"><strong>function [aii,n,ssr,Xpred,zpred,delta] = fitPowerLaw(X,z,fixn,h,mid,top,bottom) </strong></div>

<h2><a name="_description"></a>DESCRIPTION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre class="comment"> This function fits a power law of the form X = aii*z^n to the given data
 and returns the constant coefficient aii, the exponent n, and the sum of the squared residuals ssr. The user has the 
 option to fix the value of n to 1/6 by setting 'fixn' = 1.  X is the velocity cross product, z is 
 the height above bottom (monotonically increasing), and h is the flow
 depth; mid, top and bottom are TF (1,0) values that tell the code to fit the
 middle of the profile, top of the profile, and bottom of the profile, respectively.</pre></div>

<!-- crossreference -->
<h2><a name="_cross"></a>CROSS-REFERENCE INFORMATION <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
This function calls:
<ul style="list-style-image:url(../../matlabicon.gif)">
</ul>
This function is called by:
<ul style="list-style-image:url(../../matlabicon.gif)">
</ul>
<!-- crossreference -->



<h2><a name="_source"></a>SOURCE CODE <a href="#_top"><img alt="^" border="0" src="../../up.png"></a></h2>
<div class="fragment"><pre>0001 <a name="_sub0" href="#_subfunctions" class="code">function [aii,n,ssr,Xpred,zpred,delta] = fitPowerLaw(X,z,fixn,h,mid,top,bottom)</a>
0002 
0003 <span class="comment">% This function fits a power law of the form X = aii*z^n to the given data</span>
0004 <span class="comment">% and returns the constant coefficient aii, the exponent n, and the sum of the squared residuals ssr. The user has the</span>
0005 <span class="comment">% option to fix the value of n to 1/6 by setting 'fixn' = 1.  X is the velocity cross product, z is</span>
0006 <span class="comment">% the height above bottom (monotonically increasing), and h is the flow</span>
0007 <span class="comment">% depth; mid, top and bottom are TF (1,0) values that tell the code to fit the</span>
0008 <span class="comment">% middle of the profile, top of the profile, and bottom of the profile, respectively.</span>
0009 
0010 <span class="comment">% P.R. Jackson, 5-1-08</span>
0011 
0012 <span class="comment">%Example:</span>
0013 
0014 <span class="comment">% clear all</span>
0015 <span class="comment">% z = 5:50; X = 0.4512*z.^(1/6);</span>
0016 <span class="comment">% X = X + (2*rand(size(X))-1).*0.05.*X;</span>
0017 <span class="comment">% [aii,n,ssr,Xpred,zpred,delta] = fitPowerLaw(X,z,0,55,1,1,1);</span>
0018 <span class="comment">% figure(1); clf; plot(X,z); hold on</span>
0019 <span class="comment">% plot(Xpred,zpred,'r-'); hold on</span>
0020 <span class="comment">% plot(Xpred+delta,zpred,'r:',Xpred-delta,zpred,'r:'); hold on</span>
0021 
0022 zpred = [];
0023 
0024 <span class="keyword">switch</span> mid
0025     <span class="keyword">case</span> 0; zpred = [];
0026     <span class="keyword">case</span> 1; zpred = z;
0027 <span class="keyword">end</span>
0028 
0029 <span class="keyword">switch</span> top
0030     <span class="keyword">case</span> 0; zpred = zpred;
0031     <span class="keyword">case</span> 1; zpred = horzcat(zpred,linspace(z(end),h,10))';
0032 <span class="keyword">end</span>
0033 
0034 <span class="keyword">switch</span> bottom
0035     <span class="keyword">case</span> 0; zpred = zpred;
0036     <span class="keyword">case</span> 1; <span class="keyword">if</span> top; zpred = horzcat(zpred',linspace(0,z(1),100))';
0037             <span class="keyword">else</span> zpred = horzcat(zpred,linspace(0,z(1),100))';
0038             <span class="keyword">end</span>
0039 <span class="keyword">end</span>
0040         
0041 zpred = sort(zpred);
0042 
0043 <span class="keyword">switch</span> fixn
0044     <span class="keyword">case</span> 0; 
0045         coefinit   = [1 1/6]; <span class="comment">%initial guess at coefficients</span>
0046         pwrfcn     = inline(<span class="string">'coef(1)*z.^coef(2)'</span>, <span class="string">'coef'</span>, <span class="string">'z'</span>); 
0047         [coef,r,J,sig,mse] = nlinfit(z,X,pwrfcn,coefinit); <span class="comment">%nonlinear fit</span>
0048         [Xpred,delta] = nlpredci(pwrfcn,zpred,coef,r,<span class="string">'covar'</span>,sig);
0049         aii = coef(1); 
0050         n   = coef(2);
0051 
0052     <span class="keyword">case</span> 1; 
0053         coefinit   = [1]; <span class="comment">%initial guess at coefficients</span>
0054         pwrfcn     = inline(<span class="string">'coef(1)*z.^(1/6)'</span>, <span class="string">'coef'</span>, <span class="string">'z'</span>); 
0055         [coef,r,J,sig,mse] = nlinfit(z,X,pwrfcn,coefinit); <span class="comment">%nonlinear fit</span>
0056         [Xpred,delta] = nlpredci(pwrfcn,zpred,coef,r,<span class="string">'covar'</span>,sig);
0057         aii = coef(1); 
0058         n   = 1/6;
0059 <span class="keyword">end</span>
0060 
0061 ssr = sum(r.^2);
0062 
0063 
0064</pre></div>
<hr><address>Generated on Thu 21-Mar-2013 09:32:01 by <strong><a href="http://www.artefact.tk/software/matlab/m2html/" target="_parent">m2html</a></strong> &copy; 2005</address>
</body>
</html>